package com.ossean.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexHandler {
	
	public static boolean onlySpecialChar(String str){
		if(str == null)
			return true;
		if("".equals(extractEngDecimalAndChinese(str)))
			return true;
		return false;
	}
	
	public static List<String> removeOnlySpecial(List<String> list){
		for(int i = 0; i < list.size(); i++){
			String s = list.get(i);
			if(onlySpecialChar(s))
				list.remove(i);
		}
		return list;
	}
	
	public static List<String> extractEngDecimalAndChinese(List<String> list){
		List<String> result = new ArrayList<String>();
		for(String s:list){
			String s_new = extractEngDecimalAndChinese(s);
			if(!"".equals(s_new))
				result.add(s_new);
		}
		return result;
	}
	
	public static String extractEngDecimalAndChinese(String str){
		String result = "";
		//正则表达式匹配：英文、数字、中文 中文正则表达式：\u4E00-\u9FA5
		String regex = "(\\w|[\\u4E00-\\u9FA5])+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			//表示有匹配到结果
			String tmp = matcher.group(0);
			result = result + tmp + " ";
		}
		return result.trim().toLowerCase();
	}
	
	/**
	 * 匹配字符串中的中文、英文、数字、英文.号
	 * @param strs
	 * @return
	 */
	
	public static List<String> extractEngDecChiAndDot(List<String> list){
		List<String> result = new ArrayList<String>();
		for(String str:list){
			String tmp = extractEngDecChiAndDot(str);
			if(!"".equals(tmp))
				result.add(tmp);
		}
		return result;
	}
	
	public static String extractEngDecChiAndDot(String str){
		String result = "";
		//匹配一个 数字，字母，下划线，-，.，中文组成的一个字串
		String regex = "(\\w|\\-|\\+|\\'|[\\u4E00-\\u9FA5]|\\.)+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			String tmp = matcher.group(0);
			result = result + tmp + " ";
		}
		return result.trim().toLowerCase();
	}
	
	public static String[] extractEngDecimalAndChinese(String[] strs){
		String[] result_array = new String[strs.length];
		for(int i = 0; i < strs.length; i++){
			String str = strs[i];
			String result = "";
			//正则表达式匹配：英文、数字、中文 中文正则表达式：\u4E00-\u9FA5
			String regex = "(\\w|[\\u4E00-\\u9FA5])+";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(str);
			while(matcher.find()){
				//表示有匹配到结果
				String tmp = matcher.group(0);
				result = result + tmp + " ";
			}
			result_array[i] = result;
		}
		return result_array;
	}
	
	//判断字符串是否含有除小写英文外的其他字符
	public static boolean othersExceptEng(String str){
		String regex = "[^a-z]+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		if(matcher.find()){
			//表示匹配到了结果
			return true;
		}else{
			return false;
		}
	}

}
